package tothreedesignmodel.mediator;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lzhphantom
 * @description
 * @date 创建于 22:59 2020/8/1
 * @modified
 */
public class ConcreteMediator extends AbstractMediator {
    private List<AbstractColleague> colleagues = new ArrayList<>();

    @Override
    public void register(AbstractColleague cl) {
        if (!colleagues.contains(cl)) {
            colleagues.add(cl);
            cl.setMediator(this);
        }
    }

    @Override
    public void reply(AbstractColleague cl) {
        colleagues.forEach(item -> {
            if (item != cl) {
                item.receive();
            }
        });
    }

    public static void main(String[] args) {
        ConcreteMediator mediator = new ConcreteMediator();
        AbstractColleague c1 = new ConcreteColleague1();
        AbstractColleague c2 = new ConcreteColleague2();
        mediator.register(c1);
        mediator.register(c2);
        c1.send();
        System.out.println("---------------------");
        c2.send();
    }
}
